package com.gitee.wsl.func.filter

import com.gitee.wsl.func.InvokeCall

fun interface Filter<T>: InvokeCall<T, Boolean> {

    fun filter(para:T):Boolean

    override operator fun invoke(t:T) = filter(t)
}

typealias StringFilter = Filter<String>

enum class FilterType(val value:Int){
    EQ(0),BT(1),LT(-1)
}

fun<T> Comparable<T>.toFilter(type: FilterType = FilterType.EQ): Filter<T> = Filter{
    compareTo(it) == type.value
}

fun<T> Comparator<T>.toFilter(base:T,type: FilterType = FilterType.EQ): Filter<T> = Filter{
    compare(base,it) == type.value
}

fun<T> Iterator<T>.filter(block: Filter<T>):List<T> {
    val newList= mutableListOf<T>()
    forEach {
        if(block(it)) newList.add(it)
    }
    return newList
}

fun<T> Array<T>.filter(block: Filter<T>):List<T> {
    val newList= mutableListOf<T>()
    forEach {
        if(block(it)) newList.add(it)
    }
    return newList
}
